package edu.sjb.thread;

import edu.sjb.AppLauncher;
import edu.sjb.characters.Mario;
import edu.sjb.map.GenericMapElement;
import edu.sjb.map.LogicMapElement;
import edu.sjb.map.MapBrick;

public class MarioUpUpdaterThread extends Thread {
	private Mario mario;

	public MarioUpUpdaterThread(Mario m) {
		mario = m;
	}

	public void run()
	{

		for( int i = 0; i < 5; i++ )
		{
			double marioX = AppLauncher.GUI().mario().getPositionX();
			double marioY = AppLauncher.GUI().mario().getPositionY();
			int X = (int) Math.round(marioX/32);
			int Y = (int) Math.round(marioY/32);
			GenericMapElement aboveElement = AppLauncher.GUI().map().getElementAt(X,Y);
			LogicMapElement aboveMario = aboveElement.getLogicElement();
			if (!aboveMario.getName().equals(MapBrick.ELEMENT_NAME)) {
				try {
					mario.setLocation(mario.getLocation().x,
							mario.getLocation().y - 32);
					Thread.sleep(30);
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}
			else {
				MapBrick mb = (MapBrick) aboveMario;
				mb.hit(X, Y);
				AppLauncher.GUI().repaint();
			}
		}

		for( int i = 0; i < 10; i++ )
		{

			double marioX2 = AppLauncher.GUI().mario().getPositionX();
			double marioY2 = AppLauncher.GUI().mario().getPositionY();
			int X2 = (int) Math.round(marioX2/32);
			int Y2 = (int) Math.round(marioY2/32);
			LogicMapElement belowMario = AppLauncher.GUI().map().getElementAt(X2,Y2+2).getLogicElement();	
			if (!belowMario.getName().equals("Brick"))
			{
				{
					try
					{
						mario.setLocation(mario.getLocation().x,
								mario.getLocation().y + 32);
						Thread.sleep(25);
					} catch (Exception e)
					{
						e.printStackTrace();
					}
				}
			}
		}
	}

}
